//
//  CZMessageFrame.m
//  QQ聊天界面
//
//  Created by MAC on 16-3-11.
//  Copyright (c) 2016年 MAC. All rights reserved.
//

#import "CZMessageFrame.h"
#import "CZMessage.h"
#import <UIKit/UIKit.h>
#import "NSString+CZNSStringExt.h"
@implementation CZMessageFrame

//重写message的set方法
-(void)setMessage:(CZMessage *)message
{
    _message = message;
    //获取屏幕的宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    //间距的设置
    CGFloat margin = 5;
    //计算每个控件（消息时间，头像，正文）的frame和cell行高
    //1.设置时间label的frame
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    CGFloat timeW = screenW;
    CGFloat timeH = 15;
    _timeFrame = CGRectMake(timeX, timeY, timeW, timeH);
    
    //2.设置头像的frame
    CGFloat iconW = 30;
    CGFloat iconH = 30;
    CGFloat iconY = CGRectGetMaxY(_timeFrame) + margin;
    CGFloat iconX = message.type == CZMessageTypeOther?margin:screenW-margin-iconW;
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    //3.设置消息正文的frame
    //计算正文的大小
    CGSize textSize = [message.text sizeOfTextWithMaxSize:CGSizeMake(200, MAXFLOAT) font:textFont];
    //W,H值
    CGFloat textW = textSize.width+60;
    CGFloat textH = textSize.height+50;
    //先计算文字的大小nssrting类新增加一个方法。
    //X坐标值
    CGFloat textX = message.type == CZMessageTypeOther?CGRectGetMaxX(_iconFrame):(screenW-margin - iconW - textW);
    //CGFloat textX = message.type == CZMessageTypeOther?margin+iconW:screenW - margin - iconW - textW;
    //Y坐标值
    CGFloat textY = iconY;
    _textFrame = CGRectMake(textX, textY, textW, textH);
    
    //4.计算每个cell的行高
    //获取头像的最大高度值和正文的高度比较,高度值height设置会出错
    //CGFloat max = MAX(iconH, textH);
    //_rowHeight = max + margin;
    //获取头像的y值和正文的y值的最大值
    CGFloat maxY = MAX(CGRectGetMaxY(_textFrame), CGRectGetMaxY(_iconFrame));

    //CGFloat max = MAX(CGRectGetMaxY(_iconFrame), CGRectGetMaxY(_textFrame));
    _rowHeight = maxY + margin + 20;
    //_rowHeight = CGRectGetMaxY(_textFrame) + 20;
}

@end
